type 'a tree = Leaf | Node of 'a tree * 'a * 'a tree

let rec fold_left f acc = function
  | Leaf -> acc
  | Node (l, v, r) ->
     let lv = (fold_left f acc l) in
     fold_left f (f lv v) r;;

let tree0 = Node (
                Node(Leaf, 0, Leaf),
                1,
                Node(Leaf, 2, Leaf));;

let _ = fold_left (+) 0 tree0;;
let _ = fold_left (-) 0 tree0;;
let _ = List.fold_left (-) 0 [0; 1; 2];;

let rec tree_map f = function
  | Node (l, v, r) -> Node (tree_map f l, f v, tree_map f r)
  | Leaf -> Leaf

let _ = tree_map ((+) 3) tree0;;
